package controllers

import config.{Global, MyFile}
import javax.inject.Inject
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents}
import utils.TableUtils

import scala.concurrent.ExecutionContext

class ExampleController @Inject()(cc: ControllerComponents)(implicit exec: ExecutionContext) extends AbstractController(cc) with MyFile {


  def getLastzExample(num: String): Action[AnyContent] = Action { implicit request =>
    val file = s"${Global.path}/example/lastz${num}_example.fa".toFile
    Ok(Json.toJson(file.readFileToString))
  }

  def getGeneIdsExample(species: String) = Action { implicit request =>
    Ok(Json.toJson(TableUtils.genome(species).take(30).map(_ ("geneid")).mkString(",")))
  }

  def downloadExampleFile(example: String) = Action { implicit request =>
    val file = example match {
      case "blastn" => "blastn_example.fa"
      case "blastnGenome" => "blastn_example.fa"
      case "blastp" => "blastp_example.fa"
      case "muscle" => "muscle_example.fa"
      case "lastz1" => "lastz1_example.fa"
      case "lastz2" => "lastz2_example.fa"
    }

    Ok.sendFile(s"${Global.path}/example/$file".toFile).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> s"attachment; filename=$file",
      CONTENT_TYPE -> "application/x-download"
    )

  }

}
